import os
import shutil


def parse_model_data(model_list):
    str1 = '&Program_Prm' + '\n'
    str2 = '  KeyP =  '
    str3 = '&END'
    first_row = '\n'.join(['  '.join(map(str, row)) for row in model_list[0:1]])
    other_row = ''
    for i in range(len(model_list) - 1):
        other_row += '          '+''.join(['  '.join(map(str, row)) for row in model_list[i+1:i+2]]) + '\n'
    model_data = str1 + str2 + first_row + '\n' + other_row + str3 + '\n'
    return model_data


def parde_param_data(param_list):
    data = '&Geom_Phys' + '\n'
    data += '   S       = ' + str(param_list[0]) + '\n'
    data += '   b       = ' + str(param_list[1]) + '\n'
    data += '   c       = ' + str(param_list[2]) + '\n'
    data += '   amass   = ' + str(param_list[3]) + '\n'
    data += '   ajx     = ' + str(param_list[4]) + '\n'
    data += '   ajy     = ' + str(param_list[5]) + '\n'
    data += '   ajz     = ' + str(param_list[6]) + '\n'
    data += '   ajxy    = ' + str(param_list[7]) + '\n'
    data += '   ThetaT  = ' + str(0.0) + '\n'
    data += '   xbT     = ' + str(0.0) + '\n'
    data += '   zbT     = ' + str(0.0) + '\n'
    data += '&END'
    return data


def pid_file(model_list, param_list, target_path):
    model_data = parse_model_data(model_list)
    param_data = parde_param_data(param_list)
    data = model_data + param_data
    with open(target_path, 'w') as file:
        file.write(data)


def parse_plane_data(plane_list):
    str1 = '&DM' + '\n'
    str2 = '  FC =  '
    str3 = '&END'
    first_row = '\n'.join(['  '.join(map(str, row)) for row in plane_list[0:1]]) + "    " + str(plane_list[3])

    data = str1 + str2 + first_row + '\n' + str3 + '\n'
    data += '&GEO_PHY ' + '\n'
    data += '   S       = ' + str(plane_list[1][0]) + '\n'
    data += '   L       = ' + str(plane_list[1][1]) + '\n'
    data += '   BA      = ' + str(plane_list[1][2]) + '\n'
    data += '   PSIT    = ' + str(plane_list[1][3]) + '\n'
    data += '   THETAT  = ' + str(plane_list[1][4]) + '\n'
    data += '   XBT     = ' + str(plane_list[1][5]) + '\n'
    data += '   YBT     = ' + str(plane_list[1][6]) + '\n'
    data += '   ZBT     = ' + str(plane_list[1][7]) + '\n'
    data += '   IXT     = ' + str(plane_list[1][8]) + '\n'
    data += '   MASS0   = ' + str(plane_list[2][0]) + '\n'
    data += '   XCG0    = ' + str(plane_list[2][1]) + '\n'
    data += '   YCG0    = ' + str(plane_list[2][2]) + '\n'
    data += '   ZCG0    = ' + str(plane_list[2][3]) + '\n'
    data += '   IX0     = ' + str(plane_list[2][4]) + '\n'
    data += '   IY0     = ' + str(plane_list[2][5]) + '\n'
    data += '   IZ0     = ' + str(plane_list[2][6]) + '\n'
    data += '   IXY0    = ' + str(plane_list[2][7]) + '\n'
    data += '   TT0     = ' + str(plane_list[2][8]) + '\n'
    data += '   PT0     = ' + str(plane_list[2][9]) + '\n'
    data += '&END'
    return data


def cid_file(plane_list, target_path):
    data = parse_plane_data(plane_list)
    with open(target_path, 'w') as file:
        file.write(data)


def search_file(source_path):
    file_list = []
    for file in os.listdir(source_path):
        path = source_path
        try:
            path = path + os.sep + file
            if not os.path.isdir(path):
                if file.split('.')[-1] == 'dat':
                    file_list.append(path)
        except PermissionError as e:
            print(e)
    return file_list


def remove_file(file_list, target_path):
    for file in file_list:
        try:
            shutil.move(file, target_path)
        except shutil.Error as e:
            print(e)


if __name__ == '__main__':
    model_list = [[0.0]*4]*4
    param_list = [0.0]*8
    target_path = "F:\\code\\python\\pyqt5_demo\\exe_script\\PID_exe\\AeroPid_in.dat"
    pid_file(model_list, param_list, target_path)
